package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //向userinfo表中插入一条记录
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2:使用DriverManager利用驱动与数据库建立链接，得到Connection对象
        Connection connection = DriverManager.getConnection(
                //数据库地址格式:jdbc:mysql://数据库IP:端口/数据库名?一堆参数
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",//数据库用户名
                "root"//数据库密码
        );

        System.out.println("成功链接数据库!");

        //3:通过连接对象创建用于执行SQL语句的语句对象:Statement
        Statement statement = connection.createStatement();

        String sql = "INSERT INTO userinfo(username,password,nickname,age) "+
                "VALUES('张三','123456','阿三',22)";

        int num = statement.executeUpdate(sql);
        if(num>0){
            System.out.println("插入完毕");
        }

        connection.close();
    }
}
